package io.renren.modules.table.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoleilu.hutool.date.DateUtil;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.table.entity.AllocationSummaryEntity;
import io.renren.modules.table.service.CollectService;

@Controller
@RequestMapping("collect")
public class CollectController extends AbstractController {

	@Autowired
	private CollectService collectService;

	/**
	 * 初始化
	 */
	@RequestMapping("/initialization")
	@ResponseBody
	public R initialization() {
		return collectService.initialization();
	}

	/**
	 * 改变表头
	 */
	@RequestMapping("/changeJqGrid")
	@ResponseBody
	public R changeJqGrid(@RequestBody AllocationSummaryEntity allocationSummary) {
		return collectService.changeJqGrid(allocationSummary);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ResponseBody
	public R list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<Map<String, Object>> chinaJoy = collectService.queryList(query);
		int total = collectService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(chinaJoy, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}

	@RequestMapping("/export")
	public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) {
		String dateType = params.get("yearMonth").toString();
		Date sd = null;
		Date ed = null;
		SimpleDateFormat sdfwyj = null;
		SimpleDateFormat sdfpfp = null;
		if (StringUtils.equals("1", dateType)) {
			sdfwyj = new SimpleDateFormat("yyyy-MM");
			sdfpfp = new SimpleDateFormat("yyyy年MM月");
			sd = DateUtil.parse(params.get("dateMonthS").toString(), sdfwyj);
			ed = DateUtil.parse(params.get("dateMonthE").toString(), sdfwyj);
		}
		if (StringUtils.equals("2", dateType)) {
			sdfwyj = new SimpleDateFormat("yyyy");
			sdfpfp = new SimpleDateFormat("yyyy年");
			sd = DateUtil.parse(params.get("dateYearS").toString(), sdfwyj);
			ed = DateUtil.parse(params.get("dateYearE").toString(), sdfwyj);
		}
		String fileName = "收款汇总报表(" + sdfpfp.format(sd) + "至" + sdfpfp.format(ed) + ")";
		try {
			response.setContentType("octets/stream");
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1") + ".xls");
			OutputStream out = response.getOutputStream();
			collectService.export(out, params);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
